<?php
namespace app\admin\controller;

use app\common\model\validationMap;
use think\Db;
use think\Validate;

class Region extends Common
{

    public function lists(){
        $searchGoods = input('param.searchWords','');
        $list = Db::name('region')
            ->where('name','like',"%{$searchGoods}%")
            ->order('id','desc')
            ->paginate(10,[]);
        return view('',['list'=>$list]);
    }
    public function add_region(){
        if(Request()->isPost()) {
            $data = input('param.');
            $res = Validate::make()
                ->rule([
                    'name|区域名'=>"require|max:10",
                    'location|区域' =>'require',

                ])->message(['location.require'=>'请框选区域']);
            if (!$res->check($data)) {
                return  clientReturnJson('10006',$res->getError());
            }
            $array = [
                'name'=>$data['name'],
                'open'=>isset($data['open'])&&$data['open']=='on'?1:0,
                'create_time'=>date("Y-m-d H:i:s"),
            ];
            $arr = json_decode($data['location'],true);
            $inserA = [];
            $result = Db::name('region')->insertGetId($array);
            foreach ($arr as $v){
                $inserA[]  =[
                    'latitude'=>$v['lat'],
                    'longitude'=>$v['lng'],
                    'region_id'=>$result
                ];
            }
           $rs =  Db::name('region_location')->insertAll($inserA);
            return checkRes($rs,'添加');
        }
        return view('',['type'=>'add_region']);
    }

    public function edit_region(){
        if(Request()->isPost()) {
            $data = input('param.');
            $id = intval($data['id']??0);
            if(!$id) return package(10006,'id错误');
            $res = Validate::make()
                ->rule([
                    'name|区域名'=>"require|max:10",
                    'location|区域' =>'require',

                ])->message(['location.require'=>'请框选区域']);
            if (!$res->check($data)) {
                return  clientReturnJson('10006',$res->getError());
            }
            $array = [
                'name'=>$data['name'],
                'open'=>isset($data['open'])&&$data['open']=='on'?1:0,
            ];
            $arr = json_decode($data['location'],true);
            $inserA = [];
            Db::startTrans();
            $result = Db::name('region')->where('id',$id)->update($array);
            foreach ($arr as $v){
                $inserA[]  =[
                    'latitude'=>$v['lat'],
                    'longitude'=>$v['lng'],
                    'region_id'=>$id
                ];
            }
            //删掉再修改
            $res1 =  Db::name('region_location')->where('region_id',$id)->delete();
            $res2 =  Db::name('region_location')->insertAll($inserA);
            if($res1 && $res2){
                Db::commit();
                return package(0,'修改成功！');
            }else{
                Db::rollback();
                return package(10662,'修改失败！');
            }
        }else{
            $id = input('param.id');
            $info = Db::name('region')->where('id',$id)->find();
            $list = Db::name('region_location')->where('region_id',$id)->field('longitude as lng,latitude as lat')->select();
            return view('',['info'=>$info,'list'=>$list]);
        }

    }


    public function test(){
        $map = json_decode('[{"lat":31.231298103688736,"lng":104.30342674255371},{"lat":31.26534594881066,"lng":104.51079368591309},{"lat":30.910472767872786,"lng":104.43431854248047},{"lat":30.95641324406724,"lng":103.92757415771484},{"lat":31.31962097800167,"lng":104.15416717529297}]',true);
        $array = ["lat"=>31.16552,"lng"=>104.39097];//进行验证的区域
        validationMap::setCoordArray($map); //设置地图点
        var_dump(validationMap::isCityCenter($array));
    }
}